package com.itheima.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.itheima.constent.RedisMessageConstant;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Order;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtil;
import com.itheima.utils.RedisUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@DubboService
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {


    @Autowired
    private OrderSettingService orderSettingService;  //  操作  t_ordersetting 表

    @Autowired
    private MemberService memberService;  //  操作会员表

    @Override
    public Order add(Map map) {
        //   1.  判断验证码 是否 正确 或 过期
        String   codeForm = (String)map.get("validateCode");
        String   telephone = (String)map.get("telephone");
        String   orderDate = (String)map.get("orderDate");
        String   setmealId = (String)map.get("setmealId");

        Order order = null;

        // 2:  获取 redis 存储验证码
        String  codeRedis = RedisUtil.get(RedisMessageConstant.SENDTYPE_ORDER +telephone);
        if(!codeRedis.equals(codeForm)){
            throw  new  RuntimeException("验证码输入错误");
        }
        if(ObjectUtil.isEmpty(codeForm)){
            //  表单验证码必须输入
            throw  new  RuntimeException("请输入验证码");
        }

        if(ObjectUtil.isEmpty(codeRedis)){
            //  过期
            throw  new  RuntimeException("验证码过期");
        }

        //  3.  下午  下单业务处理 。。  map    体检人信息   订单信息   预约日期
          //  3  判断当天有无 预约满
        int count = orderSettingService.isOrderOkByOrderDate(orderDate);
       if(count<=0){
           throw  new  RuntimeException("预约满，请择日再约！");
       }
       //  4  当天没有预约满  ，判断有无重复预约
       Member member =  memberService.findMemberByTelephone(telephone);
        if(member!=null){
            //  老会员 。。。 重复预约  会员id  套餐id  orderdate
             int  countOrder = baseMapper.isGenerateOrderOrNot(Long.parseLong(setmealId),member.getId(),orderDate);
             if(countOrder>0){
                 throw  new  RuntimeException("请勿重复预约！");
             }else{
                 //  老会员 下单成功
               order  =   addOrder(map,member.getId());
             }
        }else{
            // 5  满足条件 可以下单 。。  考虑新会员 录入数据库即可
            member = new Member();
            member.setSex((String)map.get("sex"));
            member.setIdCard((String)map.get("idCard"));
            member.setCreateDate(new Date(System.currentTimeMillis()));
            member.setName((String)map.get("name"));
            member.setTelephone(telephone);
           long  newMemberid = memberService.saveMember(member);
           order =  addOrder(map,newMemberid);
        }
        return order;
    }

    @Override
    public Map findOrderInfoById(long id) {
        return baseMapper.findOrderInfoById(id);
    }

    /**
     *   生成订单 + 更新 预约预约数据+1
     * @param map   订单相关数据
     * @param newMemberid   会员id
     * @return
     */
    private Order addOrder(Map map, long newMemberid) {
          Order  order = new Order();
         int  orderType = (int)map.get("orderType");
         String   setmealId = (String)map.get("setmealId");
         String   orderDate = (String)map.get("orderDate");
         order.setOrderDate(DateUtil.parseDate((String)map.get("orderDate"),"yyyy-MM-dd"));
         order.setMemberId(newMemberid);
         order.setOrderType(orderType);
         order.setStatus(1);
         order.setSetmealId(Long.parseLong(setmealId));
          baseMapper.insert(order);
          orderSettingService.updateReservationsByOrderDate(orderDate);
        return  order;
    }
}
